package com.aidex.ajreport.controller;

import com.aidex.ajreport.domain.GaeaReport;
import com.aidex.ajreport.service.GaeaReportService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@RequestMapping("/ajreport/report")
@Api(tags = "报表")
public class GaeaReportController extends BaseController<GaeaReportService> {

    @GetMapping(value = "/list")
    public R list(GaeaReport entity,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<GaeaReport> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<GaeaReport> page = new Page<>(pageNo, pageSize);
        IPage<GaeaReport> pageList = service.page(page, queryWrapper);
        return R.data(pageList);
    }

    @ApiOperation(value = "添加报表")
    @PostMapping(value = "/add")
    public R add(@RequestBody GaeaReport entity) {
        service.save(entity);
        return R.success("添加成功！");
    }

    @Log(title = "编辑报表", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "编辑报表")
    @PutMapping(value = "/edit")
    public R edit(@RequestBody GaeaReport entity) {
        service.updateById(entity);
        return R.success("编辑成功!");
    }

    @Log(title = "通过id删除报表", businessType = BusinessType.DELETE)
    @ApiOperation(value = "通过id删除报表")
    @DeleteMapping(value = "/delete")
    public R delete(@RequestParam(name = "id") Long id) {
        service.removeById(id);
        return R.success("删除成功!");
    }

    @Log(title = "批量删除报表", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除报表")
    @DeleteMapping(value = "/deleteBatch")
    public R deleteBatch(@RequestParam(name = "ids") String ids) {
        service.removeByIds(Arrays.asList(ids.split(",")));
        return R.success("批量删除成功!");
    }

    @ApiOperation(value = "通过id查询报表")
    @GetMapping(value = "/{id}")
    public R queryById(@PathVariable(name = "id") Long id) {
        GaeaReport entity = service.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.data(entity);
    }
}
